<?php

namespace app\traits;

use app\modules\Admin\service\Redis as RedisService;
use Illuminate\Database\Capsule\Manager as DB;

trait stocksale
{
    // 减库存加销量（订单提交成功时执行）
    public function realForwardStockSale($order)
    {
        $items = \orderitemModel::where('order_id', $order['id'])->get();

        foreach ($items as $orderItem) {

            $goods_id = $orderItem['goods_id'];
            $goods_num = $orderItem['goods_num'];
            $goods_sku_price_id = $orderItem['goods_sku_price_id'];
            $activity_id = $orderItem['activity_id'];

            // 增加商品销量
            \goodsModel::query()->withoutGlobalScope('dianpu_id')->where('id', $goods_id)->increment('sales', $goods_num);

            // 积分商品skuprice
            if ($order['type'] == 'score') {
                $scoreGoodsSkuPrice = \scoregoodsModel::where('goods_id', $goods_id)
                    ->where('sku_price_id', $goods_sku_price_id)
                    ->first();
                $scoreGoodsSkuPrice->decrement('stock', $goods_num);
                $scoreGoodsSkuPrice->increment('sales', $goods_num);
            }

            // 非积分商品
            else {

                // 秒杀活动商品skuprice
                if ($activity_id) {
                    $seckillGoodsSkuPrice = \activitygoodsskupriceModel::where('activity_id', $activity_id)
                        ->where('goods_id', $goods_id)
                        ->where('sku_price_id', $goods_sku_price_id)
                        ->first();
                    $seckillGoodsSkuPrice->decrement('stock', $goods_num);
                    $seckillGoodsSkuPrice->increment('sales', $goods_num);
                }

                // 普通商品skuprice
                else {
                    $goodsSkuPrice = \goodsskupriceModel::where('id', $goods_sku_price_id)->first();
                    if ($goodsSkuPrice) {
                        $goodsSkuPrice->decrement('stock', $goods_num);
                        $goodsSkuPrice->increment('sales', $goods_num);
                    }
                }
            }
        }
    }

    // 加库存减销量（订单取消时执行）
    public function realFallbackStockSale($order)
    {
        $items = \orderitemModel::where('order_id', $order['id'])->get();

        foreach ($items as $orderItem) {

            $goods_id = $orderItem->goods_id;
            $goods_num = $orderItem->goods_num;
            $goods_sku_price_id = $orderItem->goods_sku_price_id;
            $activity_id = $orderItem->activity_id;

            // 减少商品销量
            \goodsModel::query()->withoutGlobalScope('dianpu_id')->where('id', $orderItem->goods_id)->decrement('sales', $goods_num);

            // 积分商品
            if ($order->type == 'score') {
                $scoreGoodsSkuPrice = \scoregoodsModel::where('goods_id', $goods_id)
                    ->where('sku_price_id', $goods_sku_price_id)
                    ->first();
                $scoreGoodsSkuPrice->increment('stock', $goods_num);
                $scoreGoodsSkuPrice->decrement('sales', $goods_num);
            }

            // 非积分商品库存
            else {

                // 秒杀活动商品库存
                if ($activity_id) {
                    $seckillGoodsSkuPrice = \activitygoodsskupriceModel::where('activity_id', $activity_id)
                        ->where('goods_id', $goods_id)
                        ->where('sku_price_id', $goods_sku_price_id)
                        ->first();
                    if ($seckillGoodsSkuPrice) {
                        $seckillGoodsSkuPrice->increment('stock', $goods_num);
                        $seckillGoodsSkuPrice->decrement('sales', $goods_num);

                        // 查询秒杀活动
                        $time     = time();
                        $activity = DB::table('activity')->find($activity_id);
                        if ($activity && $activity->status && $time > $activity->starttime && $time < $activity->endtime) {
                            $kucun_key = "activity_{$activity_id}_goods_{$goods_id}_skupriceid_{$goods_sku_price_id}";
                            RedisService::lpush($kucun_key, 1);
                            RedisService::expire($kucun_key, $activity->endtime - $time);
                        }
                    }
                }

                // 普通商品库存
                else {
                    $goodsSkuPrice = \goodsskupriceModel::where('id', $goods_sku_price_id)->first();
                    $goodsSkuPrice->increment('stock', $goods_num);
                    $goodsSkuPrice->decrement('sales', $goods_num);
                }
            }
        }
    }
}